﻿using ControleRepresentante.Logic.Models;
using NHibernate;
using NHibernate.Criterion;

namespace ControleRepresentante.Logic.Repositories.SearchExtensions
{
    public class TransportadoraJolitexSearchExtension : ISearchExtension
    {
        private readonly int? m_Codigo;

        public TransportadoraJolitexSearchExtension(int? codigo)
        {
            m_Codigo = codigo;
        }

        public ICriteria Execute(ICriteria criteria, string tableAlias)
        {
            if (m_Codigo.HasValue)
            {
                DetachedCriteria detachedCriteria = DetachedCriteria.For<TransportadoraJolitex>()
                    .SetProjection(Projections.Property<TransportadoraJolitex>(x => x.Id))
                    .Add(Restrictions.EqProperty(tableAlias + ".Id", Projections.Property<TransportadoraJolitex>(x => x.Id)));
                detachedCriteria.Add(Restrictions.Eq(Projections.Property<TransportadoraJolitex>(x => x.Codigo), m_Codigo.Value));
                criteria = criteria.Add(Subqueries.Exists(detachedCriteria));
            }
            return criteria;
        }
    }
}